package dev.werillib.android

import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.net.Uri

fun checkAppsIsExist(context: Context, packageName: String): Boolean {
    var packageInfo: PackageInfo?
    try {
        packageInfo = context.packageManager.getPackageInfo(packageName, 0)
    } catch (e: Exception) {
        packageInfo = null
        e.printStackTrace()
    }

    return packageInfo != null
}

fun openGaoDeMap(context: Context, lat: String, lon: String, addrStr: String, appName: String) {
    if (checkAppsIsExist(context, "com.autonavi.minimap")) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setPackage("com.autonavi.minimap")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.data = Uri.parse(
            "androidamap://route?sourceApplication=" + appName
                + "&sname=我的位置&dlat=" + lat
                + "&dlon=" + lon
                + "&dname=" + addrStr
                + "&dev=0&m=0&t=1"
        )
        context.startActivity(intent)
    } else {
        ToastUtil.toast(context, "高德地图未安装")
    }
}

fun openBaiduMap(context: Context, lat: String, lon: String, addrStr: String) {
    if (checkAppsIsExist(context, "com.baidu.BaiduMap")) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse("baidumap://map/geocoder?location=$lat,$lon")
        context.startActivity(intent)
    } else {
        ToastUtil.toast(context, "百度地图未安装")
    }
}

fun openTencent(context: Context, lat: String, lon: String, addrStr: String, appName: String) {
    if (checkAppsIsExist(context, "com.tencent.map")) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data =
            Uri.parse("qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=&to=$addrStr&tocoord=$lat,$lon&policy=0&referer=$appName")
        context.startActivity(intent)
    } else {
        ToastUtil.toast(context, "腾讯地图未安装")
    }
}


